using System.Windows;
using System.Windows.Data;
using FU.Capstones.IMS.Web.Common;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.DataContracts;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.EventArgs;
using FU.Capstones.IMS.Web.Silverlight.Models;
using FU.Capstones.IMS.Web.Silverlight.Popups;
using Microsoft.Practices.Prism.Commands;
using System.Linq;
using Navigation.Silverlight;

namespace FU.Capstones.IMS.Web.Silverlight.ViewModels
{
    public class RoleListViewModel : ViewModelBase
    {
        public RoleListViewModel()
        {
            Model = new RoleListModel();
            ((RoleListModel)Model).GetRoleCollectionCompleted += DoGetRoleCollectionCompleted;
            ((RoleListModel)Model).SearhRoleCompleted += DoSearhRoleCompleted;
            ((RoleListModel)Model).DeleteRoleCompleted += DoDeleteRoleCompleted;
            ((RoleListModel)Model).UndoDeleteRoleCompleted += UndoDeleteRoleCompleted;
            ((RoleListModel)Model).GetSingleRoleCompleted += DoGetSingleRoleCompleted;

            RoleCriteria = new Role();

            ServiceCallCounter += ((RoleListModel)Model).GetRoleCollectionAsync(IsShowAll);

            ErrorMessageVisibility = Visibility.Collapsed;
            MessageVisibility = Visibility.Collapsed;

            CanEditOrAdd = CurrentUser.CanEditRole;
            CanAdd = CurrentUser.CanAddRole;
        }

        #region Do Completed Methods

        void DoGetSingleRoleCompleted(object sender, SingleFeedbackCompletedEventArgs<Role> e)
        {
            ServiceCallCounter -= WebConstants.MagicNumer;
            if (e.Error != null)
            {
                Message = e.Error.Message;
                SetMessageVisibility(false);
            }
            else
            {
                Message = e.Result.Message;
                SetMessageVisibility(e.Result.IsSuccessful);
            }
        }

        void DoDeleteRoleCompleted(object sender, FeedbackCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Message = e.Error.Message;
                SetMessageVisibility(false);
            }
            else
            {
                Message = e.Result.Message;
                SetMessageVisibility(e.Result.IsSuccessful);
                if (e.Result.IsSuccessful)
                {
                    ServiceCallCounter += ((RoleListModel)Model).GetRoleCollectionAsync(IsShowAll);
                }
            }
            ServiceCallCounter -= WebConstants.MagicNumer;
        }

        void UndoDeleteRoleCompleted(object sender, FeedbackCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Message = e.Error.Message;
                SetMessageVisibility(false);
            }
            else
            {
                Message = e.Result.Message;
                SetMessageVisibility(e.Result.IsSuccessful);
                if (e.Result.IsSuccessful)
                {
                    NavigationManager.NavigateTo(WebConstants.NavigationPages.RoleDetail, currentRoleID);
                }
            }
            ServiceCallCounter -= WebConstants.MagicNumer;
        }

        void DoSearhRoleCompleted(object sender, CollectionFeedbackCompletedEventArgs<Role> e)
        {
            ServiceCallCounter -= WebConstants.MagicNumer;
            if (e.Error != null)
            {
                Message = e.Error.Message;
                SetMessageVisibility(false);
            }
            else
            {
                Message = e.Result.Message;
                SetMessageVisibility(e.Result.IsSuccessful);
                if (e.Result.IsSuccessful)
                {
                    RoleCollection = new PagedCollectionView(e.Result.Data);
                }
            }
        }

        void DoGetRoleCollectionCompleted(object sender, CollectionFeedbackCompletedEventArgs<Role> e)
        {
            ServiceCallCounter -= WebConstants.MagicNumer;
            if (e.Error != null)
            {
                Message = e.Error.Message;
                SetMessageVisibility(false);
            }
            else
            {
                Message = e.Result.Message;
                SetMessageVisibility(e.Result.IsSuccessful);
                if (e.Result.IsSuccessful)
                {
                    RoleCollection = new PagedCollectionView(e.Result.Data);
                }
            }
        }

        #endregion

        #region Commands

        #region ViewCommand

        private DelegateCommand<int?> viewCommand;
        public DelegateCommand<int?> ViewCommand
        {
            get
            {
                if (viewCommand == null)
                {
                    viewCommand = new DelegateCommand<int?>(DoViewCommand);
                }
                return viewCommand;
            }
        }

        private void DoViewCommand(int? obj)
        {
            if (obj.HasValue)
                ServiceCallCounter += ((RoleListModel)Model).GetSingleRoleAsync(obj.Value);
        }

        #endregion

        #region ShowAllCommand

        private DelegateCommand<object> showAllCommand;
        public DelegateCommand<object> ShowAllCommand
        {
            get
            {
                if (showAllCommand == null)
                {
                    showAllCommand = new DelegateCommand<object>(DoShowAllCommand);
                }
                return showAllCommand;
            }
        }

        private void DoShowAllCommand(object obj)
        {
            ServiceCallCounter +=
                ((RoleListModel)Model).GetRoleCollectionAsync(IsShowAll);
        }

        #endregion

        #region EditCommand

        private DelegateCommand<int?> editCommand;
        public DelegateCommand<int?> EditCommand
        {
            get
            {
                if (editCommand == null)
                {
                    editCommand = new DelegateCommand<int?>(DoEditCommand);
                }
                return editCommand;
            }
        }

        private void DoEditCommand(int? obj)
        {
            if (obj.HasValue)
            {
                currentRoleID = obj.Value;
                var temp = RoleCollection.Cast<Role>().FirstOrDefault(x => x.RoleID == currentRoleID);
                if (temp.IsDelete)
                {
                    var popup = new UndoDeleteConfirmationPopup();
                    popup.Show();
                    popup.Closed += (sender, e) =>
                    {
                        if (popup.DialogResult.HasValue && popup.DialogResult.Value)
                        {
                            ServiceCallCounter += ((RoleListModel)Model).UndoDeleteRoleAsync(currentRoleID);
                        }
                    };
                }
                else
                {
                    NavigationManager.NavigateTo(WebConstants.NavigationPages.RoleDetail, currentRoleID);
                }

            }
        }

        #endregion

        #region DeleteCommand

        private DelegateCommand<int?> deleteCommand;
        public DelegateCommand<int?> DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                {
                    deleteCommand = new DelegateCommand<int?>(DoDeleteCommand);
                }
                return deleteCommand;
            }
        }

        private void DoDeleteCommand(int? obj)
        {
            if (obj.HasValue)
            {
                var popup = new DeleteConfirmationPopup();
                popup.Show();
                popup.Closed += (sender, e) =>
                {
                    if (popup.DialogResult.HasValue && popup.DialogResult.Value)
                    {
                        ServiceCallCounter +=
                            ((RoleListModel)Model).DeleteRoleAsync(obj.Value);
                    }
                };
            }


        }

        #endregion

        #region CreateCommand

        private DelegateCommand<object> createCommand;
        public DelegateCommand<object> CreateCommand
        {
            get
            {
                if (createCommand == null)
                {
                    createCommand = new DelegateCommand<object>(DoCreateCommand);
                }
                return createCommand;
            }
        }

        private void DoCreateCommand(object obj)
        {
            NavigationManager.NavigateTo(WebConstants.NavigationPages.RoleDetail);
        }

        #endregion

        #region SearchCommand

        private DelegateCommand<object> searchCommand;
        public DelegateCommand<object> SearchCommand
        {
            get
            {
                if (searchCommand == null)
                {
                    searchCommand = new DelegateCommand<object>(DoSearchCommand);
                }
                return searchCommand;
            }
        }

        private void DoSearchCommand(object obj)
        {
            ServiceCallCounter += ((RoleListModel)Model).SearchRoleAsync(RoleCriteria);
            Message = WebConstants.SearchingMessage;
        }

        #endregion

        #endregion

        #region Binding Properties

        private PagedCollectionView roleCollection;
        public PagedCollectionView RoleCollection
        {
            get { return roleCollection; }
            set
            {
                if (!ReferenceEquals(roleCollection, value))
                {
                    roleCollection = value;
                    RaisePropertyChanged(() => RoleCollection);
                }
            }
        }

        private Role roleCriteria;
        public Role RoleCriteria
        {
            get { return roleCriteria; }
            set
            {
                if (!ReferenceEquals(roleCriteria, value))
                {
                    roleCriteria = value;
                    RaisePropertyChanged(() => RoleCriteria);
                }
            }
        }

        #endregion

        #region Fields

        private int currentRoleID = 0;

        #endregion

        #region Implementation of INavigationAware

        public override void OnNavigate(object extraData)
        {
            //((RoleListModel)model).GetRoleCollectionAsync();
        }

        public override void OnLeave()
        {
            //throw new NotImplementedException();
        }

        #endregion

        protected override void HandleLoadingCompleted()
        {

        }
    }
}